<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Adminlog Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class PaymentAir_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_list_booking';
                $this->load->library('bookinglib');
                $this->bookinglib = new bookinglib();
	}
        
        public function getInfoAjax($params){
                $this->db->select('count(*) AS count');
                $this->db->from($this->table_name);
                $this->db->where('payment_air','0');
                $this->db->where('payment_confirm','1');
                $this->db->where('owner',$params);
                $query = $this->db->get();
		return $query->result();
        }
        
        public function get_active_list(){
            return $this->active_list;
        }
        
        public function init_data($right){
		return array(
                                array(
						'name' => 'id',
						'label' => $this->lang->line('number'),
						'width' => '2%',
						'sort'  => 'DESC',
						'searchoptions' => false
				),array(
						'name' => 'customer',
						'label' => $this->lang->line('customer'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'booking_place_code',
						'label' => $this->lang->line('booking_place_code'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),
                                array(
						'name' => 'ship_name_customer',
						'label' => $this->lang->line('ship_name_customer'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => false
				),
				array(
						'name' => 'booking',
						'label' => $this->lang->line('booking'),
						'width' => '20%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'net_fare',
						'label' => $this->lang->line('net_fare'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'sale_fare',
						'label' => $this->lang->line('sale_fare'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'real_fare',
						'label' => $this->lang->line('real_fare'),
						'width' => '5%',
						'sort'  => FALSE,
						'searchoptions' => false
				),array(
						'name' => 'payment_air',
						'label' => $this->lang->line('payment_confirm'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => false
				),
                                array(
						'name' 	=> 'lastupdated',
						'label' => $this->lang->line('lastupdated'),
						'width' => '10%',
						'sort'  => FALSE,
						'searchoptions' => false
				),
                                array(
						'name'  => 'button',
						'width' => '10%',
						'sort'  => FALSE,
						'label'  => '',
						'searchoptions' => false
				)
		);
	}
	
	public function json_data($controller, $right){
            
                if($this->session->userdata('air_paymentAir')){
                    $arr_status = json_decode($this->session->userdata('air_paymentAir'));
                }
                
		$this->datatables
		->select("tbl.id,tbl.customerID,tbl.booking_place_code,tbl.booking_customer_name,tbl.booking,tbl.net_fare,tbl.sale_fare, tbl.real_fare, tbl.payment_air,tbl.lastupdated,bc.name AS khachhang")
		->from($this->table_name.' AS tbl')
                ->where('payment_air','0')->where('payment_confirm','1')
                ->join('book_contacts AS bc','bc.customerID = tbl.customerID');
		//->unset_column('id');
                
                $this->datatables->where('tbl.owner = '.$this->session->userdata['user_id']);
                
                if(isset($arr_status) && $arr_status->air_booking_place_code != ''){
                    $this->datatables->where('tbl.booking_place_code ',$arr_status->air_booking_place_code);
                }
                
                if(isset($arr_status) && $arr_status->air_customersName != ''){
                    $this->datatables->where('bc.name REGEXP ("'.$arr_status->air_customersName.'")');
                }

		$this->datatables->set_produce_output(false);
		$ouput = $datatables = $this->datatables->generate();
		unset($ouput['aaData']);
		$ouput['aaData'] = array();             

		foreach($datatables['aaData'] as $item){
			$ouput['aaData'][] = array(
                            		$item['id'],
					$item['khachhang'],
                                        $item['booking_place_code'],
                            		str_replace('||', '<br/>', $item['booking_customer_name']),
                                        implode("<br/>",json_decode($item['booking'])),
                                        $this->bookinglib->my_number_format($item['net_fare'],2, ',', ','),
                                        $this->bookinglib->my_number_format($item['sale_fare'],2, ',', ','),
                                        $this->bookinglib->my_number_format($item['real_fare'],2, ',', ','),
                                        ($item['payment_air'] == 1)?'Đã Thanh Toán':'Chưa thanh toán',
                                        date("d-m-Y H:i:s",$item['lastupdated']),
					$this->add_button_payment($controller, $right, $item['id'])
			);
		}
	
		return json_encode($ouput);
	}

}